package com.work.saturn.main;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.immo.libcommon.base.BaseActivity;
import com.immo.libcommon.constant.SharePreferenceKey;
import com.immo.libcommon.utils.SharePreferenceUtils;
import com.work.saturn.R;
import com.immo.libcommon.constant.RouterUtil;
import com.work.saturn.account.LoginActivity;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionNo;
import com.yanzhenjie.permission.PermissionYes;

import java.util.List;

/**
 * @author Administrator
 * @content 启动页
 * @date 2018/7/10
 */
@SuppressWarnings("unused")
@Route(path = RouterUtil.START_PAGE)
public class StartActivty extends BaseActivity {
    private static final int REQUEST_CODE_PERMISSION_MULTI = 101;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        getPerMission();

    }

    /**
     * 获取权限
     */
    public void getPerMission() {
        AndPermission.with(this)
                .requestCode(REQUEST_CODE_PERMISSION_MULTI).permission(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        ).callback(this).rationale((requestCode, rationale) -> AndPermission.rationaleDialog(StartActivty.this, rationale).show()).start();
    }

    @PermissionYes(REQUEST_CODE_PERMISSION_MULTI)
    private void getMultiYes(@NonNull List<String> grantedPermissions) {
        new Handler().postDelayed(this::isLogin, 500);
    }

    @PermissionNo(REQUEST_CODE_PERMISSION_MULTI)
    private void getMultiNo(@NonNull List<String> deniedPermissions) {
        finish();
    }

    /**
     * 是否登录
     */
    public void isLogin() {
        String token = (String) SharePreferenceUtils.get(StartActivty.this, SharePreferenceKey.TOKEN, "");
//        Intent intent;
        if (token != null && token.length() > 0) {
//            intent=new Intent(StartActivty.this,MainActivity.class);
            ARouter.getInstance().build(RouterUtil.MAIN_PAGE).withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK).navigation();
        } else {
//            intent=new Intent(StartActivty.this, LoginActivity.class);
            ARouter.getInstance().build(RouterUtil.LOGIN_PAGE).withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK).navigation();
        }
//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        startActivity(intent);
        finish();
    }


}
